<?php
namespace Admin\Controller;
use Think\Controller;
class StatisController extends CommonController {

	public function index(){
		$Customer = M('Customer');
		$Record = M('Record');

		//查询本月、上月、本周、今天、昨天胡的数据
		$searchType = I('get.searchType');

        switch($searchType){
            case 1:
            	$startDate = mktime(0,0,0,date("m"),1,date("Y"));
                $endDate = mktime(23,59,59,date('m'),date('t'),date('Y'));
            break;
            case 2:
            	//上个月
                $startDate = mktime(0,0,0,date("m")-1,1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),1,date("Y"))-1;
            break;
            case 3:
                $w = date("w");
                $startDate = mktime(0,0,0,date("m"),date("d")-$w+1,date("Y"));
                $endDate = time();
            break;
            case 4:
                // 上周
                $startDate=mktime(0,0,0,date('m'),date('d')-date('w')-7+1,date('Y'));
                $endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
            break;
            case 5:
            	// 今天
                $startDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
                $endDate = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            break;
            case 6:


                // 昨天
                $startDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"))-1;
            break;
            default:
                $startDate = 0;
                $endDate = time();
        }

        if(!empty($startDate)){
            $map['create_time'] = array('between',$startDate.','.$endDate);
        }


		$customer_new = $Customer->where( $map )->count();
		$this->assign('customer_new',$customer_new);

		$customer_old = $Customer->where( $map )->count();
		$this->assign('customer_old',$customer_old);

		$record_num = $Record->where( $map )->count();
		$this->assign('record_num',$record_num);

		//根据时间段的参数获取开始时间和结束时间
		/*$intervalArr = getTimeInterval($searchType);
		list($startDate, $endDate) = $intervalArr;
		$t = ceil((strtotime(date('Y-m-d',$endDate))-strtotime(date('Y-m-d',$startDate)))/86400)+1;
		if($t>93){
			echo '最多只能查询3个月的数据';
			exit;
		}*/

		$this->display();
	}

	public function column(){
		$Customer = M('Customer');
		$Record = M('Record');

		$year = !empty($_GET['year']) ? $_GET['year'] : date('Y');
		$month = !empty($_GET['month']) ? $_GET['month'] : date('m');
		$nowTime = mktime(0,0,0,$month,1,$year);

		$t = date("t",$nowTime);
		$startTime = mktime(0,0,0,$month,1,$year);
		$endTime = mktime(0,0,0,$month,$t,$year);


		$userId = (int)$_GET['userId'];
		$classify = $_GET['classify'];

		$customer_count = $Customer->where( $map )->count();
		$this->assign('customer_count',$customer_count);

		$map['create_time'] = array('between',array($startTime,$endTime));

		if( !empty($userId) ){
			$map['user_id'] = $userId;
		}



		$consultCount = $this->getEverydayStatistics($map);

		for($i=1;$i<=$t;$i++){
			$key = $i-1;
			$time = strtotime("$year-$month-$i");
			$count = $consultCount[date('Ymd',$time)]['count'];

			//获取统计图的数据
			$columnData[$key] = $count;
			$columnNum[$key] = $i.'日';
			$a++;
		}


		if( empty($_GET['month']) ){
			$_GET['month'] = $month;
		}
		if( empty($_GET['year']) ){
			$_GET['year'] = $year;
		}
		$this->assign('sum',array_sum($columnData));
		$this->assign('title',date('Y年m月',$nowTime).'电话记录汇总表');
		$this->assign('monthArr',array('1','2','3','4','5','6','7','8','9','10','11','12'));
		$this->assign('columnNum',json_encode($columnNum));
		$this->assign('columnData',$this->getColumnData($columnData));
		$this->assign('userList',F('userList'));
		$this->display();
	}

	public function getEverydayStatistics($where=''){
		$Record = M('Record');
		$list = $Record->where($where)->field('create_time')->limit(10000)->select();
		foreach($list as $key=>$val){
			$consultCount[date('Ymd',$val['create_time'])]['count'] += 1;
		}
		return $consultCount;
	}

	public function getColumnData($data=''){
		foreach($data as $key=>$val){
			$columnData[$key]['y'] = $val;
			$columnData[$key]['color'] = '#2f7ed8';
			$columnData[$key]['drilldown']['name'] = '点击隐藏';
			$columnData[$key]['drilldown']['categories'] = array('本日电话量');
			$columnData[$key]['drilldown']['data'] = array($val);
			$columnData[$key]['drilldown']['color'] = '#2f7ed8';
		}
		return json_encode($columnData);
	}


}